package Services

import(
	"github.com/gin-gonic/gin"
	"net/http"
	"gin/Common"
)

// 宏观数据
func DisplayOverall(c *gin.Context) {
	start_date,end_date := Common.TimelineDefaultDate()
	sql := Common.TimelineSql(Common.Timeline_overall)
	result,data := Common.GetTimelineData(sql, start_date, end_date)
	if (!result) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": "{}",
	    })
		return
	}

	c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": data,
    })

    /*
	time_now := time.Now()
	var start_date = time_now.AddDate(0, 0, -10)
	var end_date = time_now.AddDate(0, 0, 90)
    var date_str = time_now.Format("2006-01-02")
    fmt.Println(date_str) //2020-04-26 05:44:24
    fmt.Println(start_date) //2020-04-26 05:44:24

	var macros []Common.MacroData
	sql := "select Fdate,Ftype,Ftitle,Furl from macro_data where Fdate >= ? and Fdate <= ? order by Fdate,Ftype;"
	err := Databases.Db.Select(&macros, sql, start_date, end_date)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": "{}",
	    })
		fmt.Println("exec failed, ", err)
		return
	}

	fmt.Println("sql, ", sql)
	var macro_datas []Common.MacroDataContent
	//var new_types []NewType

	var date_current = ""
	var type_current = 0
	var macro_data Common.MacroDataContent
	var new_type Common.NewType
	var datacnt = 0
	for i := 0; i < len(macros); i++ {
		if macros[i].Date != date_current {
			fmt.Println("date_current:", date_current)
			if date_current != "" {
				// slice必须手动进行深拷贝
				var type_temp Common.NewType
				type_temp.Type = new_type.Type
				type_temp.News = make([]Common.New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
				copy(type_temp.News, new_type.News) 
				macro_data.Newtypes = append(macro_data.Newtypes, type_temp)

				// slice必须手动进行深拷贝
				var macro_data_temp Common.MacroDataContent
				macro_data_temp.Date = macro_data.Date
				macro_data_temp.Newtypes = make([]Common.NewType, len(macro_data.Newtypes[:len(macro_data.Newtypes):len(macro_data.Newtypes)]))
				copy(macro_data_temp.Newtypes, macro_data.Newtypes) 

		    	macro_datas = append(macro_datas, macro_data_temp)
		    	fmt.Println("title, ", macro_datas[0].Newtypes[0].News[0].Title)
		    	fmt.Println("date, ", macro_datas[0].Date)
		    	datacnt++
		    }
			macro_data.Date = macros[i].Date
			new_type.News = new_type.News[0:0]
			macro_data.Newtypes = macro_data.Newtypes[0:0]
			new_type.Type = Common.GetOverallType(macros[i].Type)
			date_current = macros[i].Date
			type_current = macros[i].Type
		} else if type_current != macros[i].Type {
			// slice必须手动进行深拷贝
			var type_temp Common.NewType
			type_temp.Type = new_type.Type
			type_temp.News = make([]Common.New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
			copy(type_temp.News, new_type.News)

			macro_data.Newtypes = append(macro_data.Newtypes, type_temp)
			new_type.Type = Common.GetOverallType(macros[i].Type)
			new_type.News = new_type.News[0:0]
			type_current = macros[i].Type
		}
		var new = Common.New{macros[i].Title, macros[i].Url}
		new_type.News = append(new_type.News, new)
    }
    if date_current != "" {
    	// slice必须手动进行深拷贝
		var type_temp Common.NewType
		type_temp.Type = new_type.Type
		type_temp.News = make([]Common.New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
		copy(type_temp.News, new_type.News) 
		macro_data.Newtypes = append(macro_data.Newtypes, type_temp)
    	
    	// slice必须手动进行深拷贝
		var macro_data_temp Common.MacroDataContent
		macro_data_temp.Date = macro_data.Date
		macro_data_temp.Newtypes = make([]Common.NewType, len(macro_data.Newtypes[:len(macro_data.Newtypes):len(macro_data.Newtypes)]))
		copy(macro_data_temp.Newtypes, macro_data.Newtypes)
    	//macro_data.Newtypes = append(macro_data.Newtypes, macro_data_temp)

    	macro_datas = append(macro_datas, macro_data)
    }

    str, _ := json.Marshal(macro_datas)
    str_res := string(str)
    //fmt.Println("str:", str)
    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": str_res,
    })
    */
}

